//
//  XLAccountSafeView.m
//  industry
//
//  Created by xl on 2020/12/9.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLAccountSafeView.h"
#import "XLAccountSafeViewProtocol.h"
#import "XLAccountSafeAdapter.h"
#import "XLAccountSafePresenterProtocol.h"

@interface XLAccountSafeView ()<XLAccountSafeViewProtocol, XLAdapterDelegate>

@property (nonatomic, strong) AUToast *toastLoad;
@property (nonatomic, strong) XLTableView *tableView;
@property (nonatomic, strong) XLAccountSafeAdapter *adapter;
@property (nonatomic, strong) XLView *footerView;

@end
@implementation XLAccountSafeView

- (void)reloadData{
    NSArray *functions = XLSend(self.baseController.presenter, XLAccountSafePresenterProtocol, getFunctions);
    [_adapter setAdapterArray:functions];
    [_tableView reloadData];

}

- (void)dealloc {
    XLLog(@"XLAccountSafeView dealloc");
}

#pragma mark - XLAccountSafeViewProtocol
- (void)buildView {
    [self addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    NSArray *functions = XLSend(self.baseController.presenter, XLAccountSafePresenterProtocol, getFunctions);
    [_adapter setAdapterArray:functions];
    [_tableView reloadData];
}

- (void)showLoadingAnimalWithTitle:(NSString *)title {
    self.toastLoad = [XLToast showLoadingTitle:title superView:self];
}

- (void)hideLoadingAnimal {
    if (self.toastLoad) {
        [self.toastLoad dismissToast];
    }
}

#pragma mark - XLAdapterDelegate
- (void)didSelectCellData:(id)cellData {
    XLSend(self.baseController.presenter, XLAccountSafePresenterProtocol, handleCellData:cellData);
}

#pragma mark - UIButton
- (void)clickExit {
    XLSend(self.baseController.presenter, XLAccountSafePresenterProtocol, requestLoginOut);
}

#pragma mark - Setter/getter
- (XLTableView *)tableView {
    if (!_tableView) {
        _tableView = [[XLTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteBgColor];
        _tableView.delegate = self.adapter;
        _tableView.dataSource = self.adapter;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

- (XLAccountSafeAdapter *)adapter {
    if (!_adapter) {
        _adapter = [[XLAccountSafeAdapter alloc] init];
        _adapter.adapterDelegate = self;
    }
    return _adapter;
}

- (XLView *)footerView {
    if (!_footerView) {
        _footerView = [[XLView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        _footerView.backgroundColor = [UIColor whiteColor];
        
        XLButton *button = [[XLButton alloc] initWithFrame:CGRectMake(30, 50, SCREEN_WIDTH - 30*2, 45)];
        button.layer.cornerRadius = 12.0f;
        button.layer.masksToBounds = YES;
        button.backgroundColor = [UIColor grayThirdBgColor];
        button.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:14];
        [button setTitleColor:[UIColor blackFontColor] forState:UIControlStateNormal];
        [button setTitle:@"退出登录" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickExit) forControlEvents:UIControlEventTouchUpInside];
        
        [_footerView addSubview:button];
    }
    return _footerView;
}

@end
